$(function(){
	
//修改页面和添加页面

requestSourceMut();
requestPortMut();






//向后台请求渠道信息
function requestSourceMut()
{	
	
	var url="getAllInfomationSource.do";//渠道地址
	var data={};
	var object={};
	data=YuXi.util.getAsynCache(url,"null",function(data){
		object=data.object;
		$("#s0").html("");
		xxx(object,"#souceMut");
		
	},function(){});//请求数据
	
}
//查询向后台请求分口信息
function requestPortMut()
{	var url="getAllInformationPort.do";//分口地址
	var data={};
	var object={};
	data=YuXi.util.getAsynCache(url,null,function(data){
		object=data.object;
		//用data循环添加分口信息	
		$("#s0").html("");
		xxx(object,"#fenkMut");
	},function(){});//请求数据
	
}


function xxx(object,className)
{

	var a=[];
	var flag=0;
	
	var flaga={};
	
function treeMenu(a){
    this.tree=a||[];
    this.groups={};
};
treeMenu.prototype={
    init:function(superId){
    
        this.group();
        return this.getDom(this.groups[superId]);
    },
    group:function(){
    	
        for(var i=0;i<this.tree.length;i++){
            if(this.groups[this.tree[i].superId]){
                this.groups[this.tree[i].superId].push(this.tree[i]);
            }else{
                this.groups[this.tree[i].superId]=[];
                this.groups[this.tree[i].superId].push(this.tree[i]);
            }
        }

    },

    getDom:function(a){
        if(!a){return ''}

       flag=0;
        var html="";
    	if(a[0].superId==0)
    	{
    		for(var i=0;i<a.length;i++)
    		{
    			if(className=="#fenkMut")
    			{
    				html='<optgroup id="s'+a[i].id+'" label="'+a[i].portName+'"><option data-label="'+a[i].portName+'" data-super='+a[i].superId+' value="'+a[i].id+'">&nbsp;&nbsp;'+a[i].portName+'</option></optgroup>';
    			}
    			else
    			{
    				html='<optgroup id="s'+a[i].id+'" label="'+a[i].name+'"><option data-label="'+a[i].name+'" data-super='+a[i].superId+' value="'+a[i].id+'">&nbsp;&nbsp;'+a[i].name+'</option></optgroup>';
    			
    			}
    			$("#s0").append(html);
    			
    		}
    		flag=1;
    	}
    	
    	if(flag==0)
    	{
    		
    			
    			var length=a.length;

    			for(var i=0;i<length;i++)
    			{
    			
    			
    				var xx=a[i].id;
    				var asuperid=a[i].superId;
    				var aname="";
    				if(className=="#fenkMut")
    				{
    					aname=a[i].portName;
    				}
    				else
    				{
    					aname=a[i].name;
    				}
    				var biao=0;
    				x=1;
    				var flagx=space(asuperid,object);
    				
    				var spacea="";//添加空格
    				for(var m=0;m<=flagx;m++)
    				{
    					spacea=spacea+"&nbsp;";
    				}
    				
    				for(var j=0;j<object.length;j++)
    				{

    					if(xx==object[j].superId)
    					{
    						html='<optgroup id="s'+xx+'" label="&nbsp;'+spacea+aname+'"><option data-label="'+aname+'" data-super='+asuperid+' value="'+xx+'">&nbsp;'+spacea+aname+'</option></optgroup>';
    						
    						$("#s"+asuperid).append(html);
    						biao=1;
    						break;
    					}

    				}

    				if(biao==0)
    				{
    					html='<option data-label="'+aname+'"  data-super='+asuperid+' value="'+xx+'">&nbsp;'+spacea+aname+'</option>';
    					
    				    $("#s"+asuperid).append(html);
    				}
    					
    			}
    		
    	}
    	 
        for(var i=0;i<a.length;i++){
        	if(className=="#fenkMut")
        	{
        		 flaga={id:a[i].id,name:a[i].portName,superid:a[i].superid};
        	}
       		else
       		{
       			flaga={id:a[i].id,name:a[i].name,superid:a[i].superid};
       		}
        	
           this.getDom(this.groups[a[i].id]);

        };

        /*html+='</optgroup>\n';*/

       /* $("#"+id).append(html);*/
        return html;
    }
};
	var html=new treeMenu(object).init(0);
	var hh=$("#s0").html();
	var html="";
	if(className=="#fenkMut")
	{
		html='';
	}
	else
	{
		html='';
	}
	$(className).html(html);
	$(className).append(hh);
}
var x=1;	//向后台请求渠道信息
function space(superid,object)
{

	if(superid==0)
	{

		return 0;
	}
	for(var i=0;i<object.length;i++)
	{
		if(object[i].id==superid)
		{
			
			x=space(object[i].superId,object);
			
		}
	}
	return x+1;

}














	
})